package com.example.day08upload;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CircleProgressBar mCpb;
    private Button mBtn;
    private boolean down;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mCpb = (CircleProgressBar) findViewById(R.id.cpb);
        mBtn = (Button) findViewById(R.id.btn);

        mBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                download();
                break;
        }
    }

    private void download() {
        if (!down){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 101; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        down = true;
                        if (i == 100){
                            down = false;
                        }
                        mCpb.setProgress(i);
                    }
                }
            }).start();
        }

    }
}